package com.roin.learn.activiti.controller;

import com.github.pagehelper.util.StringUtil;
import com.roin.learn.activiti.base.BaseController;
import com.roin.learn.activiti.entity.Page;
import com.roin.learn.activiti.entity.Position;
import com.roin.learn.activiti.entity.ResourceResult;
import com.roin.learn.activiti.service.PositionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 职位维护模块Controller
 */
@Controller
@RequestMapping("/position")
public class PositionController extends BaseController{
    @Resource
    private PositionService positionService;

    @RequestMapping("")
    public String toMain(HttpSession session, Model model){
        checkLogin(session);
        return "position/main";
    }

    @RequestMapping("/add.do")
    @ResponseBody
    public ResourceResult add(Position position) {
        ResourceResult result = new ResourceResult();
        if (position == null)
            result.setMessage("添加职位失败!position！");
        else {
            if (StringUtil.isEmpty(position.getPositionNo()))
                result.setMessage( "添加职位失败!positionNo参数为空！");
            if (StringUtil.isEmpty(position.getPositionMc()))
                result.setMessage( "添加职位失败!positionMc参数为空！");
        }
        if (StringUtil.isEmpty(result.getMessage())) {
            int row = positionService.add(position);
            result.setSuccess(row > 0);
            result.setMessage(row > 0 ? "添加职位成功！" : "添加职位失败!");
        }
        return result;
    }


    @RequestMapping("/deleteByIds.do")
    @ResponseBody
    public ResourceResult delete(Integer[] ids) {
        ResourceResult result = new ResourceResult();
        if (ids==null || ids.length==0)
            result.setMessage( "删除职位失败!ids参数为空！");
        if (StringUtil.isEmpty(result.getMessage())) {
            int row = positionService.deleteByIds(ids);
            result.setSuccess(row > 0);
            if (row==0)
                result.setMessage("删除职位失败!");
            else if (ids.length!=row) {
                result.setMessage(String.format("共删除%s条数据，删除失败%s条！", row, ids.length - row));
                result.setSuccess(true);
            }
            else {
                result.setMessage("删除职位成功!");
                result.setSuccess(true);
            }
        }
        return result;

    }

    @RequestMapping("/update.do")
    @ResponseBody
    public ResourceResult update(Position position) {
        ResourceResult result = new ResourceResult();
        if (position == null)
            result.setMessage("修改职位失败!参数为空！");
        else{
            if (position.getId()==null)
                result.setMessage( "修改职位失败!id参数为空！");
            if (StringUtil.isEmpty(position.getPositionNo()))
                result.setMessage( "修改职位失败!positionNo参数为空！");
            if (StringUtil.isEmpty(position.getPositionMc()))
                result.setMessage( "修改职位失败!positionMc参数为空！");
        }
        if (StringUtil.isEmpty(result.getMessage())) {
            int row = positionService.update(position);
            result.setSuccess(row > 0);
            result.setMessage(row > 0 ? "修改职位成功！" : "修改职位失败!");
        }

        return result;
    }

    @RequestMapping("/getById.do")
    @ResponseBody
    public ResourceResult getById(@RequestParam("id") Integer id) {
        ResourceResult result = new ResourceResult();
        Position position = positionService.getById(id);
        if (position==null)
            result.setMessage("找不到ID为"+id+"职位");
        else {
            result.setSuccess(true);
            result.setData(position);
        }
        return result;
    }

    @RequestMapping("/list.json")
    @ResponseBody
    public Page list(Integer pageNum, Integer pageSize) {
        return positionService.getPage(pageNum,pageSize,null);
    }
}
